﻿function GetDialogInstance(id, title, url, height, width, callback, istop, screenunit) {
    if (screenunit) {
        if (istop) {
            height = (window.top.document.body.clientHeight - 44) * height / 100 + "px";
            width = window.top.document.body.clientWidth * width / 100 + "px";
        } else {
            height = (window.document.body.clientHeight - 44) * height / 100 + "px";
            width = window.document.body.clientWidth * width / 100 + "px";
        }
    } else {
        if (height > window.top.document.body.clientHeight) {
            height = window.top.document.body.clientHeight
        }
        if (width > window.document.body.clientWidth) {
            width = window.document.body.clientWidth;
        }
        height = (height - 44) + 'px';
        width = width + 'px';
    }
    //处理url，判断是否要拼接
    var handleurl = "";
    if (url.substr(0, 7) == "http://" || url.substr(0, 1) == "/" || url.substr(0, 11) == "javascript:") {
        handleurl = url
    } else {
        handleurl = GetRootPath() + url
    }
    //url带上dialog的id
    if (handleurl.indexOf("?") > 0) {
        handleurl += "&dialogid=" + id;
    } else {
        handleurl += "?dialogid=" + id;
    }

    var template = "";
    template += "<div class=\"fast-modal-bg\" id='" + id + "' >";
    template += "    <div class=\"fast-modal\" style='width:" + width + "'>";
    template += "        <div class=\"fast-modal-title\">";
    template += "            <div class=\"fast-modal-title-lable\"> <div class=\"icon_current\"></div><span>当前位置 > " + title + "</span></div>";
    template += "            <div  class=\"icon_close\" onclick=CloseDialogWithOutReturn('" + id + "'," + istop + ")></div>";
    template += "        </div>";
    template += "        <div class=\"fast-modal-content\">";
    template += "            <iframe id='" + id + "'_fastdialogiframe  src='" + handleurl + "' class=\"fast-modal-iframe\" style='height:" + height + "'></iframe>";
    template += "        </div>";
    template += "    </div>";
    template += "</div>";
    var dialoginstace = {}
    dialoginstace.template = template;
    dialoginstace.id = id;
    dialoginstace.title = title;
    dialoginstace.url = handleurl;
    dialoginstace.height = height;
    dialoginstace.width = width;
    dialoginstace.callbackevent = function (RetrunValue) {
        if (callback) {
            if (RetrunValue) {
                callback(RetrunValue);
            } else {
                callback();
            }
        }
    }
    return dialoginstace;
}

function GetAlertInstance(dialogtype, message, callback, btnclosetext) {
    var id = UUID();
    var btntext = "确定"
    var title = "温馨提示";
    if (btnclosetext) {
        btntext = btnclosetext;
    }
    var titleclass = "fast-modal-title";
    var iconclass = "fast-modal-alert-icon "
    switch (dialogtype) {
        case "success":
            titleclass = "fast-modal-title-success";
            iconclass = "fast-modal-success-icon "
            break;
        case "waring":
            titleclass = "fast-modal-title-waring";
            iconclass = "fast-modal-waring-icon "
            break;
        case "fail":
            titleclass = "fast-modal-title-fail";
            iconclass = "fast-modal-fail-icon "
            break;
    }

    var template = "";
    template += "<div class=\"fast-modal-alert-bg\" id=" + id + " >";
    template += "    <div class=\"fast-modal-alert\" >";
    template += "        <div class=" + titleclass + ">";
    template += "            <div class=\"fast-modal-title-lable\"> <div class=\"icon_current\"></div><span>" + title + "</span></div>";
    template += "            <div  class=\"icon_close\" onclick=CloseAlert('" + id + "')></div>";
    template += "        </div>";
    template += "        <div class=\"fast-modal-alert-content\">";
    template += "            <div class='" + iconclass + "' ></div>";
    template += "            <label class=\"fast-modal-alert-content-label\" >" + message + "</label>";
    template += "        </div>";
    template += "        <div class=\"fast-modal-alert-footer\">";
    template += "            <button  class=\"btn\" onclick=CloseAlertWithCallBack('" + id + "')  style='width:100px'>" + btntext + "</button>";
    template += "        </div>";
    template += "    </div>";
    template += "</div>";

    var dialoginstace = {}
    dialoginstace.template = template;
    dialoginstace.id = id;
    dialoginstace.callbackevent = function () {
        window.top.document.getElementById(id).remove();
        if (callback) {
            callback();
        }
    }
    if (window.top.dialoglist) {
        for (var i = 0; i < window.top.dialoglist.length; i++) {
            if (window.top.dialoglist[i].id == id) {
                window.top.dialoglist.splice(i, 1);
                break;
            }
        }
        var dialogobj = {
            "id": id,
            "instance": dialoginstace
        };
        window.top.dialoglist.push(dialogobj)
    } else {
        var dialoglist = [];
        var dialogobj = {
            "id": id,
            "instance": dialoginstace
        };
        dialoglist.push(dialogobj);
        window.top.dialoglist = dialoglist;
    }
    return dialoginstace;
}

function GetConfirmInstance(message, okcallback, btnoktext, btncanceltext) {
    var id = UUID();
    var oktext = "确定";
    var canceltext = "取消";
    var title = "温馨提示";
    if (btnoktext) {
        oktext = btnoktext;
    }
    if (btncanceltext) {
        canceltext = btncanceltext
    }
    var titleclass = "fast-modal-title";
    var iconclass = "fast-modal-confirm-icon"
    var template = "";
    template += "<div class=\"fast-modal-alert-bg\" id=" + id + " >";
    template += "    <div class=\"fast-modal-alert\" >";
    template += "        <div class=" + titleclass + ">";
    template += "            <div class=\"fast-modal-title-lable\"> <div class=\"icon_current\"></div><span>" + title + "</span></div>";
    template += "            <div  class=\"icon_close\" onclick=CloseAlert('" + id + "')></div>";
    template += "        </div>";
    template += "        <div class=\"fast-modal-alert-content\">";
    template += "            <div class='" + iconclass + "' ></div>";
    template += "            <label class=\"fast-modal-alert-content-label\" >" + message + "</label>";
    template += "        </div>";
    template += "        <div class=\"fast-modal-alert-footer\">";
    template += "            <button  class=\"btn\" onclick=CloseAlert('" + id + "')  style='width:100px'>" + canceltext + "</button>";
    template += "            <button  class=\"btn btn-primary\" onclick=CloseAlertWithCallBack('" + id + "')  style='width:100px;margin-left:10px'>" + oktext + "</button>";
    template += "        </div>";
    template += "    </div>";
    template += "</div>";

    var dialoginstace = {}
    dialoginstace.template = template;
    dialoginstace.id = id;
    dialoginstace.callbackevent = function () {
        window.top.document.getElementById(id).remove();
        if (okcallback) {
            okcallback();
        }
    }
    if (window.top.dialoglist) {
        for (var i = 0; i < window.top.dialoglist.length; i++) {
            if (window.top.dialoglist[i].id == id) {
                window.top.dialoglist.splice(i, 1);
                break;
            }
        }
        var dialogobj = {
            "id": id,
            "instance": dialoginstace
        };
        window.top.dialoglist.push(dialogobj)
    } else {
        var dialoglist = [];
        var dialogobj = {
            "id": id,
            "instance": dialoginstace
        };
        dialoglist.push(dialogobj);
        window.top.dialoglist = dialoglist;
    }
    return dialoginstace;
}

function OpenDialog(title, url, height, width, callback, params, screenunit) {
    var id = UUID();
    var dialog = GetDialogInstance(id, title, url, height, width, callback, false, screenunit);
    var dialogobj = document.getElementById("fastdialogdiv" + id);
    if (!dialogobj) {
        dialogobj = document.createElement("div");
        dialogobj.id = "fastdialogdiv" + id;
    }
    dialogobj.innerHTML = dialog.template
    document.body.appendChild(dialogobj);
    if (window.dialoglist) {
        for (var i = 0; i < window.dialoglist.length; i++) {
            if (window.dialoglist[i].id == id) {
                window.dialoglist.splice(i, 1);
                break;
            }
        }
        var dialogobj = {
            "id": id,
            "instance": dialog
        };
        if (params) {
            dialogobj.fastdialog_params = params
        }
        window.dialoglist.push(dialogobj)
    } else {
        var dialoglist = [];
        var dialogobj = {
            "id": id,
            "instance": dialog
        };
        if (params) {
            dialogobj.fastdialog_params = params
        }
        dialoglist.push(dialogobj);
        window.dialoglist = dialoglist;
    }
}

function OpenTopDialog(title, url, height, width, callback, params, screenunit) {
    var id = UUID();
    var dialog = GetDialogInstance(id, title, url, height, width, callback, true, screenunit);
    var dialogobj = window.top.document.getElementById("fastdialogdiv" + id);
    if (!dialogobj) {
        dialogobj = window.top.document.createElement("div");
        dialogobj.id = "fastdialogdiv" + id;
    }
    dialogobj.innerHTML = dialog.template
    window.top.document.body.appendChild(dialogobj);
    if (window.top.dialoglist) {
        for (var i = 0; i < window.top.dialoglist.length; i++) {
            if (window.top.dialoglist[i].id == id) {
                window.top.dialoglist.splice(i, 1);
                break;
            }
        }
        var dialogobj = {
            "id": id,
            "instance": dialog
        };
        if (params) {
            dialogobj.fastdialog_params = params
        }
        window.top.dialoglist.push(dialogobj)
    } else {
        var dialoglist = [];
        var dialogobj = {
            "id": id,
            "instance": dialog
        };
        if (params) {
            dialogobj.fastdialog_params = params
        }
        dialoglist.push(dialogobj);
        window.top.dialoglist = dialoglist;
    }

}

function CloseDialog(ReturnValue) {
    var id = GetUrlParam("dialogid");
    if (id) {
        var obj = window.top.document.getElementById(id);
        if (obj) {
            for (var i = 0; i < window.top.dialoglist.length; i++) {
                if (window.top.dialoglist[i].id == id) {
                    if (ReturnValue) {
                        window.top.dialoglist[i].instance.callbackevent(ReturnValue);
                    } else {
                        window.top.dialoglist[i].instance.callbackevent();
                    }
                    window.top.dialoglist.splice(i, 1);
                    break;
                }
            }
            window.top.document.getElementById("fastdialogdiv" + id).remove();
        } else {
            if (ReturnValue) {
                window.parent.dialoglist[0].instance.callbackevent(ReturnValue);
            } else {
                window.parent.dialoglist[0].instance.callbackevent();
            }
            window.parent.dialoglist.splice(0, window.parent.dialoglist.length);
            document.getElementById("fastdialogdiv" + id).remove();
        }
    } else {
        if (ReturnValue) {
            window.parent.dialoglist[0].instance.callbackevent(ReturnValue);
        } else {
            window.parent.dialoglist[0].instance.callbackevent();
        }
        window.parent.dialoglist.splice(0, window.parent.dialoglist.length);
        var modallist = window.parent.document.getElementsByClassName("fast-modal-bg");
        for (var i = 0; i < modallist.length; i++) {
            modallist[i].remove();
        }

    }
}

function CloseDialogWithOutReturn(id, istop) {
    if (id) {
        if (istop) {
            for (var i = 0; i < window.top.dialoglist.length; i++) {
                if (window.top.dialoglist[i].id == id) {
                    window.top.dialoglist.splice(i, 1);
                    break;
                }
            }
            window.top.document.getElementById("fastdialogdiv" + id).remove();
        } else {
            for (var i = 0; i < window.parent.dialoglist.length; i++) {
                if (window.parent.dialoglist[i].id == id) {
                    window.parent.dialoglist.splice(i, 1);
                    break;
                }
            }
            document.getElementById("fastdialogdiv" + id).remove();
        }
    }

}

function CloseAlert(id) {
    var obj = window.top.document.getElementById(id);
    if (obj) {
        for (var i = 0; i < window.top.dialoglist.length; i++) {
            if (window.top.dialoglist[i].id == id) {
                window.top.dialoglist.splice(i, 1);
                break;
            }
        }
        window.top.document.getElementById(id).remove();
    }
}

function CloseAlertWithCallBack(id) {
    var obj = window.top.document.getElementById(id);
    if (obj) {
        for (var i = 0; i < window.top.dialoglist.length; i++) {
            if (window.top.dialoglist[i].id == id) {
                window.top.dialoglist[i].instance.callbackevent();
                window.top.dialoglist.splice(i, 1);
                break;
            }
        }
      
    }
}

function GetParams() {
    var id = GetUrlParam("dialogid");
    var params = "undefined";
    if (id) {
        if (window.top.dialoglist) {
            for (var i = 0; i < window.top.dialoglist.length; i++) {
                if (window.top.dialoglist[i].id == id) {
                    params = window.top.dialoglist[i].fastdialog_params
                    break;
                }
            }
        }
        if (window.dialoglist) {
            for (var i = 0; i < window.dialoglist.length; i++) {
                if (window.dialoglist[i].id == id) {
                    params = indow.dialoglist[i].fastdialog_params
                    break;
                }
            }
        }
    } else {
        if (window.top.dialoglist && window.top.dialoglist.length > 0) {
            params = window.top.dialoglist[window.top.dialoglist.length - 1].fastdialog_params
        }
        if (window.dialoglist && window.dialoglist.length > 0) {
            params = window.dialoglist[window.dialoglist.length - 1].fastdialog_params
        }
    }

    return params;
}

function OpenAlert(message, callback, btnclosetext) {
    var dialog = GetAlertInstance("alert", message, callback, btnclosetext);
    var dialogobj = window.top.document.getElementById("fastalertdiv");
    if (!dialogobj) {
        dialogobj = window.top.document.createElement("div");
        dialogobj.id = "fastalertdiv";
    }
    dialogobj.innerHTML = dialog.template
    window.top.document.body.appendChild(dialogobj);
}

function OpenSuccess(message, callback, btnclosetext) {
    var dialog = GetAlertInstance("success", message, callback, btnclosetext);
    var dialogobj = window.top.document.getElementById("fastalertdiv");
    if (!dialogobj) {
        dialogobj = window.top.document.createElement("div");
        dialogobj.id = "fastalertdiv";
    }
    dialogobj.innerHTML = dialog.template
    window.top.document.body.appendChild(dialogobj);
}

function OpenWaring(message, callback, btnclosetext) {
    var dialog = GetAlertInstance("waring", message, callback, btnclosetext);
    var dialogobj = window.top.document.getElementById("fastalertdiv");
    if (!dialogobj) {
        dialogobj = window.top.document.createElement("div");
        dialogobj.id = "fastalertdiv";
    }
    dialogobj.innerHTML = dialog.template
    window.top.document.body.appendChild(dialogobj);
}

function OpenFail(message, callback, btnclosetext) {
    var dialog = GetAlertInstance("fail", message, callback, btnclosetext);
    var dialogobj = window.top.document.getElementById("fastalertdiv");
    if (!dialogobj) {
        dialogobj = window.top.document.createElement("div");
        dialogobj.id = "fastalertdiv";
    }
    dialogobj.innerHTML = dialog.template
    window.top.document.body.appendChild(dialogobj);
}

function OpenConfirm(message, okcallback, btnoktext, btncanceltext) {
    var dialog = GetConfirmInstance(message, okcallback, btnoktext, btncanceltext);
    var dialogobj = window.top.document.getElementById("fastconfirmdiv");
    if (!dialogobj) {
        dialogobj = window.top.document.createElement("div");
        dialogobj.id = "fastconfirmdiv";
    }
    dialogobj.innerHTML = dialog.template
    window.top.document.body.appendChild(dialogobj);
}



